package XMLParser;
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
 
public class myHandler extends DefaultHandler{
        private ParsedData myPD = new ParsedData();
        private String str="";
        private String locName="";

        public ParsedData getParsedData() {
                return this.myPD;
        }

        @Override
        public void startDocument() throws SAXException {
        	this.myPD = new ParsedData();
        }
 
        @Override
        public void endDocument() throws SAXException {
        }
 
        /** Gets be called on opening tags like: 
         * <tag> 
         * Can provide attribute(s), when xml was like:
         * <tag attribute="attributeValue">*/
        @Override
        public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        	double lng,lat=0;
        	String attrValueLong, attrValueLat="";
        	if (localName.equals("place")) {// Extract an Attribute
        		attrValueLat = atts.getValue("lat");
        		attrValueLong = atts.getValue("lon");
        		lat = Double.parseDouble(attrValueLat);
        		lng = Double.parseDouble(attrValueLong);
        		myPD.setLat(lat);
        		myPD.setLong(lng);
        	}
        	locName=localName;
        	str="";
        }


		/** Gets called on the following structure: 
         * <tag>characters</tag> */
        @Override
        public void characters(char ch[], int start, int length) {
        	for (int j = start; j < start + length; j++) {
        		str=str + ch[j];
        	}
        	if (locName=="house_number"){
        		myPD.setHouseNumber(Integer.parseInt(str));
        	}
      		if (locName=="road"){
        		myPD.setStreet(str);
        	}
        	if (locName=="city"){
        		myPD.setCity(str);
        	}
        	if (locName=="country"){
        		myPD.setCountry(str);
        	}
        }
    }

